<?php
class BaseModelFileLog extends BaseModelLog {
    /**
     * write log
     * 
     * @param string $msg
     * @param int $type
     * @return bool, true on success, false on failure
     */
    public static function write($msg, $type = LOG_WARNING) {
		$type = self::getType($type);
		$filename = LOGS_PATH . $type . '.log';
		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $msg . "\n";
		$log = new File($filename, true);
		if ($log->writable()) {
			$log->append($output);
			return $log->close();
		}
		return false;
	}
	/**
	 * read log by type, only read the lastest msg
	 * 
	 * @param int $type
	 * @return string msg or null
	 */
	public static function read($type = LOG_WARNING) {
	    $type = self::getType($type);
	    $filename = LOGS_PATH . $type . '.log';
	    $log = new File($filename, false);
	    if ($log->readable()) {
	        $msg = $log->read(4096, 'a+', true);
	        $log->close();
	        return $msg;
	    }
	    return NULL;
	}
	/**
	 * truncate the log
	 * 
	 * @param int $type
	 * @return bool, true on success, false on failure
	 */
	public static function truncate($type = LOG_WARNING) {
	    $type = self::getType($type);
	    $filename = LOGS_PATH . $type . '.log';
	    $log = new File($filename, false);
	    if ($log->writeable()) {
	        return $log->delete();
	    }
	    return false;
	}
}